package com.zzy.weixin.service.customer;


import com.zzy.weixin.entity.Customer;
import com.zzy.weixin.repository.CustomerRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author zhangzhiyi on 2017/6/7.
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Autowired
    CustomerRepository customerRepository;

    @Override
    public List<Customer> findByLastName(String name) {
        return customerRepository.findByLastName(name);
    }

    @Override
    public Customer findOne(String id) {
        return customerRepository.findOne(id);
    }

    @Override
    public Customer add(Customer customer) {
        Customer user = customerRepository.getByFirstName(customer.getFirstName());
        if(user!=null){
            throw new IllegalArgumentException("用户名已存在");
        }
        return customerRepository.save(customer);
    }

    @Override
    public List<Customer> finAll() {
        List<Customer> customerList = new ArrayList<>();
        for (Iterator<Customer> iterator = customerRepository.findAll().iterator(); iterator.hasNext();) {
            customerList.add(iterator.next());
        }
        return customerList;
    }

}
